package com.bwie.task;

import cn.hutool.core.bean.BeanUtil;
import com.bwie.es.EsVisitor;
import com.bwie.pojo.Visitor;
import com.bwie.service.VisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Component
public class task {

    @Autowired
    private VisitorService visitorService;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Scheduled(cron = "0/30 * * * * ?")
    public void taskVisitor(){
        System.out.println("开启同步");
        List<Visitor> visitorsList = visitorService.visltorList();
        for (Visitor visitor : visitorsList) {
            EsVisitor esVisitor = BeanUtil.toBean(visitor, EsVisitor.class);
            elasticsearchRestTemplate.save(esVisitor);
        }
        System.out.println("关闭同步");
    }

}
